﻿#include "mainwindow.h"
#include "ui_mainwindow.h"

#define PI 3.1415926

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    initPlot();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::initPlot()
{
    //链接信号和槽，为了同步左侧y轴和右侧y轴的范围，在本示例里其实也是为了好看
    connect(ui->customPlot->yAxis2, SIGNAL(rangeChanged(const QCPRange&)), ui->customPlot->yAxis, SLOT(setRange(const QCPRange&)));

    //隐藏左侧y轴的数字刻度
    ui->customPlot->yAxis->setTickLabels(false);
    //显示右侧的y轴
    ui->customPlot->yAxis2->setVisible(true);
    //添加余弦图像,并将这个图像和下方x轴，右侧第一根y轴绑定
    ui->customPlot->addGraph(ui->customPlot->xAxis, ui->customPlot->yAxis2);
    //设置图像颜色方便区分
    ui->customPlot->graph(0)->setPen(QPen(Qt::black));
    //设置图像名称用于图例显示
    ui->customPlot->graph(0)->setName(QStringLiteral("余弦函数"));
    //右侧新增一个y轴
    ui->customPlot->axisRect()->addAxis(QCPAxis::atRight);
    //添加正弦图像，并将这个图像和下方x轴，右侧第二根y轴绑定
    ui->customPlot->addGraph(ui->customPlot->xAxis, ui->customPlot->axisRect()->axis(QCPAxis::atRight, 1));
    ui->customPlot->graph(1)->setName(QStringLiteral("正弦函数"));
    //为了方便区分设置右侧第二根y轴为蓝色
    ui->customPlot->axisRect()->axis(QCPAxis::atRight, 1)->setBasePen(QPen(Qt::blue));
    ui->customPlot->axisRect()->axis(QCPAxis::atRight, 1)->setTickPen(QPen(Qt::blue));
    //设置图例显示
    ui->customPlot->legend->setVisible(true);
    //将图例显示放在图像的下方，为了防止遮挡图像
    ui->customPlot->plotLayout()->addElement(1, 0, ui->customPlot->legend);
    //设置不显示边框（因为同时显示边框和图例在图像下方，那么边框只会显示左上的，影响观看效果）
    ui->customPlot->legend->setBorderPen(Qt::NoPen);
    //设置每个曲线的图例优先铺满列
    ui->customPlot->legend->setFillOrder(QCPLayoutGrid::foColumnsFirst);
    //设置伸缩因子
    ui->customPlot->plotLayout()->setRowStretchFactor(1, 0.01);
    //设置两个y轴的范围，因为正弦和余弦函数值域都是[-1, 1]
    ui->customPlot->yAxis2->setRange(-1, 1);
    ui->customPlot->axisRect()->axis(QCPAxis::atRight, 1)->setRange(-1, 1);
    //设置交互，可以点击图例实现曲线加粗和曲线对应的y轴加粗的效果
    ui->customPlot->setInteractions(QCP::iSelectLegend);
    //设置自能选中图例的item
    ui->customPlot->legend->setSelectableParts(QCPLegend::spItems);
    //设置x轴名称
    ui->customPlot->xAxis->setLabel(QStringLiteral("X"));
    ui->customPlot->xAxis->setRange(0, 20);

    //设置x轴左边有一个箭头(好看)
    ui->customPlot->xAxis->setLowerEnding(QCPLineEnding::esSpikeArrow);

    //timer初始化
    m_timer = new QTimer();
    m_timer->start(10);

    m_x = 0.0;

    //tag绑定对应的y轴
    m_tag1 = new AxisTag(ui->customPlot->yAxis);
    m_tag1->setPen(ui->customPlot->graph(0)->pen());
    m_tag2 = new AxisTag(ui->customPlot->axisRect()->axis(QCPAxis::atRight, 1));
    m_tag2->setPen(ui->customPlot->graph(1)->pen());

    this->resize(1000, 480);

    //设置两个y轴的间距，以便能够看清楚y轴游标
    ui->customPlot->yAxis2->setPadding(30);
    ui->customPlot->axisRect()->axis(QCPAxis::atRight, 1)->setPadding(30);

    ui->customPlot->legend->setMargins(QMargins(20, 0, 0, 10));

    //链接信号和槽
    connect(ui->customPlot, &QCustomPlot::selectionChangedByUser, this, &MainWindow::dealSelection);
    connect(m_timer, &QTimer::timeout, this, &MainWindow::dealTimeout);
}

void MainWindow::dealSelection()
{
    for(int i = 0; i < ui->customPlot->legend->itemCount(); i++)
    {
        QPen basePen = ui->customPlot->axisRect()->axis(QCPAxis::atRight, i)->basePen();
        QPen graphPen = ui->customPlot->graph(i)->pen();
        basePen.setWidth(1);
        graphPen.setWidth(1);
        QCPPlottableLegendItem *item = ui->customPlot->legend->itemWithPlottable(ui->customPlot->graph(i));
        if(item->selected())
        {
            basePen.setWidth(3);
            graphPen.setWidth(3);
        }
        ui->customPlot->axisRect()->axis(QCPAxis::atRight, i)->setBasePen(basePen);
        ui->customPlot->graph(i)->setPen(graphPen);
    }
    //重新绘制才能显示效果
    ui->customPlot->replot();
}

void MainWindow::dealTimeout()
{
    double cValue = cos(m_x / (2.0 * PI));
    double sValue = sin(m_x / (2.0 * PI));

    //更新两个游标的位置和显示的值
    m_tag1->updatePosition(cValue);
    m_tag2->updatePosition(sValue);
    m_tag1->setText(QString::number(cValue, 'f', 2));
    m_tag2->setText(QString::number(sValue, 'f', 2));

    //为对应的图像添加对应的数据
    ui->customPlot->graph(0)->addData(m_x, cValue);
    ui->customPlot->graph(1)->addData(m_x, sValue);

    m_x += 0.1;

    if(m_x > 20)
    {
        ui->customPlot->xAxis->setRange(m_x, 20, Qt::AlignRight);
    }

    ui->customPlot->replot();
}
